package com.itheima.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SortMapper;
import com.itheima.pojo.BlogSort;
import com.itheima.utils.DateUtils;
import com.itheima.utils.IdWorker;
import com.itheima.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
public class SortService {

    @Autowired
    private SortMapper sortMapper;

    public void add(BlogSort blogSort) {
        if (blogSort.getUid() == null) {
            blogSort.setUid(String.valueOf(new IdWorker().nextId()));
        }
        if (blogSort.getStatus() == null) {
            blogSort.setStatus(1);
        }
        if (blogSort.getSort() == null) {
            blogSort.setSort(0);
        }
        if (blogSort.getContent() == null) {
            blogSort.setContent("");
        }
        if (blogSort.getSortName() == null) {
            blogSort.setSortName("");
        }
        blogSort.setClickCount(0);
        Date time = DateUtils.strToDateTime(DateUtils.getNowTime());
        blogSort.setCreateTime(time);
        blogSort.setUpdateTime(time);
        sortMapper.insertSelective(blogSort);
    }

    public void edit(BlogSort blogSort) {
        Date time = DateUtils.strToDateTime(DateUtils.getNowTime());
        blogSort.setUpdateTime(time);
        sortMapper.updateByPrimaryKeySelective(blogSort);
    }

    public void deleteBatch(List<BlogSort> list) {
        if (list != null && list.size() > 0) {
            for (BlogSort blogSort : list) {
                if (blogSort.getUid() != null) {
                    sortMapper.deleteByPrimaryKey(blogSort.getUid());
                }
            }
        }
    }

    public PageResult<BlogSort> getList(BlogSort blogSort) {
        PageHelper.startPage(blogSort.getCurrentPage(), blogSort.getPageSize());
        Page<BlogSort> sorts = (Page<BlogSort>) sortMapper.selectAll();
        return new PageResult<BlogSort>(sorts.getTotal(), sorts.getResult());
    }

    public void stick(BlogSort blogSort) {
        int max = sortMapper.findMaxSort();
        BlogSort sort1 = sortMapper.selectByPrimaryKey(blogSort.getUid());
        Integer sort = sort1.getSort();
        if (sort <= max) {
            sort1.setSort(max + 1);
        }
        sortMapper.updateByPrimaryKey(sort1);
    }

    public void blogSortByCite() {
        List<BlogSort> sorts = sortMapper.selectAll();
        for (BlogSort blogSort : sorts) {
            blogSort.setSort(0);
            sortMapper.updateByPrimaryKeySelective(blogSort);
            blogSort.setSort(sortMapper.citeNum(blogSort.getUid()));
            sortMapper.updateByPrimaryKeySelective(blogSort);
        }
    }

    public void blogSortByClickCount() {
        List<BlogSort> blogSorts = sortMapper.selectAll();
        for (BlogSort blogSort : blogSorts) {
            blogSort.setSort(blogSort.getClickCount());
            sortMapper.updateByPrimaryKeySelective(blogSort);
        }
    }

}
